# Read build options (default is ifort-mkl.opt)
PLATFORM ?= tintin-ifort-mkl
include build_options/$(PLATFORM).opt

ifneq (,$(findstring -D__USE_CUDA,$(DFLAGS)))
 CUDA = YES
else
 CUDA = NO
endif

# Lists of things to build
.SUFFIXES:
.SUFFIXES: .cu .c .f90 .o
OBJS = MCMC_data.o MCMC_utils.o MCMC_subroutines.o hierarchical_MCMC.o
ifeq ($(CUDA), YES)
 OBJS += MCMC_c_bindings.o MCMC_c_functions.o
 APP = MCMC.bin.cuda
else
 APP = MCMC.bin
endif

# Set of compiler and linker options
FCFLAGS = $(DFLAGS) $(OPTFLAGS) $(OPENMP) $(NETCDFINCLUDE)
CFLAGS = $(DFLAGS) $(OPTFLAGS) $(OPENMP)
NVFLAGS = -Xcompiler '$(DFLAGS) $(OPTFLAGS) $(OPENMP)'
LDFLAGS = $(OPTFLAGS) $(OPENMP) $(MATHLIBS) $(NETCDFLIBS) 
ifeq ($(CUDA), YES) 
 LDFLAGS += $(CUDALIBS)
endif

# Default rule, build the binary
$(APP) : clean $(OBJS)
	$(LD) -o $@ $(OBJS) $(LDFLAGS)

# Remove all build products
clean :
	rm -f *.o *.mod $(APP)

# Dependencies
DEPS = MCMC_data.o MCMC_utils.o MCMC_subroutines.o
ifeq ($(CUDA), YES)
 DEPS += MCMC_c_bindings.o
endif
hierarchical_MCMC.o : $(DEPS)
MCMC_subroutines.o : MCMC_data.o MCMC_utils.o

# Rule for compiling f90 files
.f90.o:
	$(FC) $(FCFLAGS) -c $< -o $@

# Rule for compiling c files
.c.o:
	$(CC) $(CFLAGS) -c $< -o $@

# Rule for compiling CUDA c files
.cu.o:
	$(NVCC) $(NVFLAGS) -c $< -o $@

